#include "NextHandle.h"
#include "PKB.h"

using namespace std;

NextHandle::NextHandle(void)
{
}

NextHandle::~NextHandle(void)
{
}
std::vector<std::vector<PROG_LINE>> NextHandle::RetrieveNext(
        std::vector<PROG_LINE> prev_in, std::vector<PROG_LINE> next_in)
{
		std::sort(next_in.begin(), next_in.end());
        std::vector<std::vector<PROG_LINE>> result_vector;
        std::vector<PROG_LINE> next_out;
        std::vector<PROG_LINE> prev_out;

		std::set<PROG_LINE> temp;
		std::vector<PROG_LINE> found;
        unsigned int i;
        
		for (i = 0; i < prev_in.size(); i++)
        {
			PROG_LINE_ARRAY next_temp = PKB::next.GetNext(prev_in[i]);
			for (auto it=next_temp.begin();it!=next_temp.end();it++)
			{
				PROG_LINE pl = *it;
				if (std::binary_search(next_in.begin(), next_in.end(), pl))
                        {
                                prev_out.push_back(prev_in[i]);
                                next_out.push_back(pl);
                        }
			}
        }
        result_vector.push_back(prev_out);
        result_vector.push_back(next_out);
        
		return result_vector;
}

std::vector<std::vector<PROG_LINE>> NextHandle::RetrieveTransitiveNext(
	std::vector<PROG_LINE> prev_in, std::vector<PROG_LINE> next_in)
{
		std::sort(next_in.begin(), next_in.end());
        std::vector<std::vector<PROG_LINE>> result_vector;
        std::vector<PROG_LINE> next_out;
        std::vector<PROG_LINE> prev_out;

		std::set<PROG_LINE> temp;
		std::vector<PROG_LINE> found;
        unsigned int i;
        
		for (i = 0; i < prev_in.size(); i++)
        {
			PROG_LINE_ARRAY next_temp = PKB::next.GetTransitiveNext(prev_in[i]);
			for (auto it=next_temp.begin();it!=next_temp.end();it++)
			{
				PROG_LINE pl = *it;
				if (std::binary_search(next_in.begin(), next_in.end(), pl))
                        {
							    prev_out.push_back(prev_in[i]);
                                next_out.push_back(pl);
                        }
			}
        }
        result_vector.push_back(prev_out);
        result_vector.push_back(next_out);
        
		return result_vector;
}